﻿using linkedin_bot_demo.Common;
using log4net;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace linkedin_bot_demo
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private readonly static ILog _log;
        public static ILog Log { get { return _log; } }

        private readonly string _accountsFile;
        public string AccountsFile { get { return this._accountsFile; } }

        static App()
        {
            _log = LogManager.GetLogger(typeof(App));
        }

        public App()
        {
            this.Dispatcher.UnhandledException += onDispatcherUnhandledException;

            this._accountsFile = Path.Combine(
                Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), 
                "accounts.xml"
                );
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Logger.Setup();
        }

        void onDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            string errorMessage = string.Format("An unhandled exception occurred: {0}", e.Exception.Message);
            Log.Error(errorMessage, e.Exception);
            e.Handled = true;

            MessageBox.Show(errorMessage, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}
